package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.ShopHistoryWords;
import com.kgc.scd.vo.ShopHistoryWordsVO;
import com.kgc.scd.mapper.HistoryMapper;
import com.kgc.scd.service.HistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HistoryServiceimpl implements HistoryService {
    @Autowired(required = false)
    private HistoryMapper historyMapper;
    @Override
    public int intsert(ShopHistoryWordsVO shopHistoryWordsVO) {

      ShopHistoryWords shopHistoryWords=new ShopHistoryWords();
        BeanUtils.copyProperties(shopHistoryWordsVO,shopHistoryWords);

        return historyMapper.insert(shopHistoryWords);
    }

    @Override
    public List<ShopHistoryWordsVO> getHistoryWords() {
        QueryWrapper<ShopHistoryWords>queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        List<ShopHistoryWords> list =historyMapper.selectList(queryWrapper);
        List<ShopHistoryWordsVO>listvo=list.stream().map(ca->{
            ShopHistoryWordsVO shopHistoryWordsVO=new ShopHistoryWordsVO();
            BeanUtils.copyProperties(ca,shopHistoryWordsVO);
            return shopHistoryWordsVO;
        }).collect(Collectors.toList());
        return listvo;
    }
    @Override
    public boolean delete(List<String >words) {
        log.info("传过来的值：{}",words);
        //查询集合列表
        QueryWrapper<ShopHistoryWords>queryWrapper=new QueryWrapper<>();
        List<ShopHistoryWords> list =historyMapper.selectList(queryWrapper);
        //查询集合中所有的words列表
        List<String> lsitwords = list.stream().map(ShopHistoryWords::getWords).collect(Collectors.toList());
        log.info("{}",lsitwords);

        //在java中我们可以通过contains方法判断集合中是否已经有某个值 list.contains()；
        //判断一个集合是否包含另一个集合中所有元素list.containsAll(list1)
        try {
            if (lsitwords.containsAll(words)){
                words.forEach(wd->{
                    log.info("wd的值为：{}",wd);
                    QueryWrapper<ShopHistoryWords>queryWrapper1=new QueryWrapper<>();
                    queryWrapper1.eq("words",wd);
                    historyMapper.delete(queryWrapper1);
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  true;
    }

}
